package com.example.mobile2030606148tdh0511w;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private TextView tvMaintitle;
    private ImageView ivMainBanner;
    private TextView tvMainContent;

    final String IMAGEPATH="http://10.110.147.222:3000/web/images/banner.jpg";
//            final String TEXTPATH="http://192.168.1.100:8000/web/text.txt";
//    final String IMAGEPAT="http://10.110.147.222:3000/post";
//    final String TEXTPATH="http://10.110.147.222"
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        new Thread(){
            @Override
            public void run(){
                super.run();
                try {
                    URL imageUrl=new URL(IMAGEPATH);
                    HttpURLConnection imageConnection=(HttpURLConnection)imageUrl.openConnection();
                    imageConnection.setRequestMethod("GET");
                    imageConnection.setConnectTimeout(5000);
                    InputStream imageIS=imageConnection.getInputStream();
                    final Bitmap bitmap= BitmapFactory.decodeStream(imageIS);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ivMainBanner.setImageBitmap(bitmap);
                        }
                    });
                    imageIS.close();
                    imageConnection.disconnect();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void initView() {
        tvMaintitle = (TextView) findViewById(R.id.tvMaintitle);
        ivMainBanner = (ImageView) findViewById(R.id.ivMainBanner);
        tvMainContent = (TextView) findViewById(R.id.tvMainContent);
    }
}